<?php

class TagInput {
	var $name = "";
	var $object = null;
	
	public function __construct($object,$name,$style='',$disabled=false) {
		// TODO check parms
		$this->object= $object;
		$this->name= $name;
		$msg = $object->getErrorDescription($name);
		$display = (in_array($name,$object->getErrors()))?"inline":"none";
		
		print "<img class='errorImg' src='images/error.png' style='display:".$display.";' title='".$msg."' alt='".$msg."' />" 
		."<label class='labelText' >". $this->object->getLabel($name). "</label>"
		. "<input name='".$this->name."' id='".$name."'"." class='text'".(($style!='')?" style='$style' ":"")
		." type='text' value='".$this->object->getValue($name)."'".($disabled?" disabled='disabled' ":"")." ></input>";
	}
}

class TagCombo {
	var $name = "";
	var $object = null;

	public function __construct($object,$table,$name,$first,$event=''){
		// TODO check parms
		$this->name= $name;
		$this->table= $table;
		$this->object= $object;
		
		$msg = $object->getErrorDescription($name);
		if($first)
			$options="<option value=''></option>";
		else 
			$options = "";
		$keys = array_keys($table);
		foreach ($keys as $i ){
			//$val = $this->object->getValue($name);
			if(method_exists($this->object, 'getValue'))
				$val = $this->object->getValue($name);
			else 
				$val = $name;
			$options .= "<option value='".$table[$i]."' "
			.(($val!='' && $val==$i)?" selected ":""). ">"
			.htmlspecialchars($i,ENT_QUOTES)."</option>";
		}
		
		if(method_exists($object,'getErrors'))
			$display = (in_array($name,$object->getErrors()))?"inline":"none";
		else 
			$display="inline-block";
			
		if(method_exists($object,'getLabel'))
			$label=$this->object->getLabel($name);
		else 
			$label="";
			
		print "<img class='errorImg' src='images/error.png' style='display:".$display
		.";' title='".$msg."' alt='".$msg."' />"
		. "<label class='labelCombo' >". $label . "</label>"
		. "<select name='".$this->name."' id='".$name."'".($event?" onchange='".$event."'":"")." >"
		. $options
		. "</select>";
	}

}

class TagRadio {
	var $name = "";
	var $object = null;

	public function __construct($object,$table,$name ){
		// TODO check parms
		$this->object= $object;
		$this->name= $name;
		$this->table= $table;
		
		$val = $this->object->getValue($name);
		$display = (in_array($name,$object->getErrors()))?"inline":"none";
		$msg = $object->getErrorDescription($name);
		$input = "<img class='errorImg' src='images/error.png' style='display:".$display.";' title='".$msg."' alt='".$msg."' />"
				."<label class='labelRadio' >". $this->object->getLabel($name). "</label>";
		$keys = array_keys($table);
		foreach ($keys as $i ){
			// $this->object->getValue($name)
			$input .= "<input name='".$this->name."' id='".$name."'"
			." type='radio' value='".$i."'".($val!='' && $val==$i?" checked":"")
			." >".htmlspecialchars($table[$i],ENT_QUOTES)."</input>";
		}
		if($val=='')
			$input .= "<input name='".$this->name."' type='radio' value='' style='display:none' checked ></input>";
		print $input;
	}

}

class TagCheckbox {
	var $name = "";
	var $checked = false;
	var $object = null;
	
	public function __construct($object,$name,$style='',$disabled=false,$event='') {
		// TODO check parms
		$this->object= $object;
		$this->name= $name;
		
		$val = $this->object->getValue($name);
		$msg = $object->getErrorDescription($name);
		$display = (in_array($name,$object->getErrors()))?"inline":"none";
		$label=$this->object->getLabel($name);
		$input = "<input name='".$this->name."' id='".$name."' value=1"
			." type='checkbox' ".($val?" checked":"").($event?" onchange='".$event."'":"")
			." >"."<label class='labelRadio' >".$label."</label></input>";
		print $input;
	}
}

class TagTextarea {
	var $name = "";
	var $object = null;
	var $cols=40;
	var $rows = 20;
	
	public function __construct($object,$name,$cols,$rows,$style='',$disabled=false) {
		// TODO check parms
		$this->object= $object;
		$this->name= $name;
		
		$msg = $object->getErrorDescription($name);
		$display = (in_array($name,$object->getErrors()))?"inline":"none";
		$label=$this->object->getLabel($name);
		$input = "<textarea name='".$this->name."' id='".$name."'" .(($style!='')?" style='$style' ":"")
			." rows=$rows cols=$cols"
			." >".$this->object->getValue($name)."</textarea>";
		print $input;
	}
}

class ComboTableFactory {

	// TODO var $url = null;
	var $db = null;
	var $user = null;
	var $pass = null;
	
	public function __construct($host, $dbname , $user , $pass){
		try {
			$this->db = new PDO('mysql:host='.$host.';dbname='.$dbname, $user, $pass ,
				array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''));
			$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		} catch (PDOException $e) {
			print "Error!: " . $e->getMessage() . "<br/>";
			die();
		}
	}
	
	public function comboTable($table,$key,$value,$filter=""){
		try {
			$ar = array();
			if($this->db!=null){
				
				$sql = "SELECT ".$key." , ".$value." FROM ".$table;
				if($filter!='')
					$sql .= "  WHERE ".$filter;
				$stmt = $this->db->prepare($sql);
				
				$stmt->execute();
				
				while ($row = $stmt->fetch()) {
    				$ar[$row[0]]=$row[1];
  				}
  				
  				/*
  				print("<!--");
  				var_dump($ar);
  				print("-->");
  				*/
  				
			}
		}catch (PDOException $e) {
			print "PDOException!: " . $e->getMessage() . "<br/>";
			die();
		} catch (Exception $e) {
   			print "Exception!: " . $e->getMessage() . "<br/>";
   			die();
		}
		
		return $ar;
	}
	
	// TODO generalizzare parametri
	public function parameterTable($table,$p1,$p2,$p3,$filter=""){
		try {
			$ar = array();
			if($this->db!=null){
	
				$sql = "SELECT ".$p1." , ".$p2." , ".$p3." FROM ".$table;
				if($filter!='')
					$sql .= "  WHERE ".$filter;
				$stmt = $this->db->prepare($sql);
	
				$stmt->execute();
	
				while ($row = $stmt->fetch()) {
					$ar[$row[0]]=array($row[1],$row[2]);
				}
				/*
				print("<!--");
				var_dump($ar);
				print("-->");
				*/
			}
		}catch (PDOException $e) {
			print "PDOException!: " . $e->getMessage() . "<br/>";
			die();
		} catch (Exception $e) {
			print "Exception!: " . $e->getMessage() . "<br/>";
			die();
		}
	
		return $ar;
	}
	
	public function close(){
		$this->db=null;
	}
	
}

?>